package org.chz.system;

import javafx.util.Pair;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

@Component
public class Dictionary {

    private List<Pair<String,Integer>> dict;
    private Map<String,Set<Integer>> index;



    public Dictionary(){
        this.index = new HashMap<>();
        this.dict = new ArrayList<>();
    }

    @PostConstruct
    void initDictionary(){
        readDictFile();
        readIndexFile();
    }
//    读取词典索引
    private void readIndexFile(){
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("dictIndex.dat");
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {

            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split("\\s+");
                if (parts.length < 2) {
                    continue; // 跳过格式不正确的行
                }

                HashSet<Integer> values = new HashSet<>();
                for (int i = 1; i < parts.length; i++) {
                    values.add(Integer.parseInt(parts[i]));
                }

                index.put(parts[0], values);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    读取词典
    private void readDictFile() {
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("dict.dat");
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split("\\s+");
                if (parts.length != 2) {
                    continue; // 跳过格式不正确的行
                }
                String word = parts[0];
                int number = Integer.parseInt(parts[1]);
                dict.add(new Pair<>(word, number));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void printData() {
        for (String key : index.keySet()) {
            System.out.print(key + ": ");
            for (Integer value : index.get(key)) {
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }
    public void printPairList() {
        for (Pair<String, Integer> pair : dict) {
            System.out.println(pair.getKey() + ": " + pair.getValue());
        }
    }

    public List<Pair<String, Integer>> getDict() {
        return dict;
    }

    public Map<String, Set<Integer>> getIndex() {
        return index;
    }

    // 方法用于根据ArrayList<String>中的字符串找到所有对应String的HashSet的并集并返回
    public HashSet<Integer> findUnion(List<String> keys) {
        HashSet<Integer> unionSet = new HashSet<>(); // 用于存储并集结果

        // 遍历ArrayList中的每个字符串
        for (String key : keys) {
            // 如果HashMap中包含该字符串，则将对应的HashSet添加到并集中
            if (index.containsKey(key)) {
                unionSet.addAll(index.get(key));
            }
        }

        return unionSet; // 返回并集结果
    }
}
